package com.microcity.autocomponent.autoview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RadioButton;

import com.microcity.autocomponent.R;

/**
 * Created by Darksiderl on 2017/1/10.
 * 注释：要设置clickble为true才可以被选中
 * 或者设置监听显然不如设置clickble简单
 */
public class YDRadioButton extends RadioButton {

    private int mDrawableLeftSize;// xml文件中设置的大小
    private int mDrawableTopSize;// xml文件中设置的大小
    private int mDrawableRightSize;// xml文件中设置的大小
    private int mDrawableBottomSize;// xml文件中设置的大小

    public YDRadioButton(Context context) {
        this(context, null, 0);
    }

    public YDRadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public YDRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.YDRadioButton);

        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            Log.i("YDRadioButton", "attr:" + attr);
            switch (attr) {
                case R.styleable.YDRadioButton_drawableRightSize:
                    mDrawableRightSize = a.getDimensionPixelSize(R.styleable.YDRadioButton_drawableRightSize, 50);
                    Log.i("YDRadioButton", "mDrawableSize:" + mDrawableRightSize);
                    break;
                case R.styleable.YDRadioButton_drawableLeftSize:
                    mDrawableLeftSize = a.getDimensionPixelSize(R.styleable.YDRadioButton_drawableLeftSize, 50);
                    Log.i("YDRadioButton", "mDrawableSize:" + mDrawableLeftSize);
                    break;
                case R.styleable.YDRadioButton_drawableTopSize:
                    mDrawableTopSize = a.getDimensionPixelSize(R.styleable.YDRadioButton_drawableTopSize, 50);
                    Log.i("YDRadioButton", "mDrawableSize:" + mDrawableTopSize);
                    break;
                case R.styleable.YDRadioButton_drawableBottomSize:
                    mDrawableBottomSize = a.getDimensionPixelSize(R.styleable.YDRadioButton_drawableBottomSize, 50);
                    Log.i("YDRadioButton", "mDrawableSize:" + mDrawableBottomSize);
                    break;
                case R.styleable.YDRadioButton_drawableydRight:
                    drawableRight = a.getDrawable(attr);
                    break;
                case R.styleable.YDRadioButton_drawableydTop:
                    drawableTop = a.getDrawable(attr);
                    break;
                case R.styleable.YDRadioButton_drawableydLeft:
                    drawableLeft = a.getDrawable(attr);
                    break;
                case R.styleable.YDRadioButton_drawableydBottom:
                    drawableBottom = a.getDrawable(attr);
                    break;
                default:
                    break;
            }
        }
        a.recycle();

        setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);

    }

    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left,
                                                        Drawable top, Drawable right, Drawable bottom) {

        if (left != null) {
            left.setBounds(0, 0, mDrawableLeftSize, mDrawableLeftSize);
        }
        if (right != null) {
            right.setBounds(0, 0, mDrawableRightSize, mDrawableRightSize);
        }
        if (top != null) {
            top.setBounds(0, 0, mDrawableTopSize, mDrawableTopSize);
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, mDrawableBottomSize, mDrawableBottomSize);
        }
        setCompoundDrawables(left, top, right, bottom);
    }
}
